//
//  BCTitleInputView.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/12/10.
//  Copyright © 2018 guoshikeji. All rights reserved.
//

#import "BCTitleInputView.h"

static CGFloat BCTitleInputViewTopPadding = 20.0;
static CGFloat BCTitleInputViewTitleHeight = 30.0;
static CGFloat BCTitleInputViewInputHeight = 35.0;

@implementation BCTitleInputView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupSubviews];
        //
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(inputTextFieldTextDidChanged:) name:UITextFieldTextDidChangeNotification object:self.inputTextField];
    }
    return self;
}

- (void)setupSubviews {
    self.titleLabel = [QMUILabel new];
    self.titleLabel.textColor = [UIColor qmui_colorWithHexString:@"#333333"];
    self.titleLabel.font = [UIFont systemFontOfSize:13 weight:UIFontWeightRegular];
    self.titleLabel.adjustsFontSizeToFitWidth = true;
    self.titleLabel.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 10);
    [self addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self).offset(BCTitleInputViewTopPadding);
        make.left.mas_equalTo(self).offset(10.0);
        make.right.mas_equalTo(self).offset(10.0);
        make.height.mas_equalTo(BCTitleInputViewTitleHeight);
    }];
    //
    self.inputTextField = [QMUITextField new];
    self.inputTextField.textColor = self.titleLabel.textColor;
    self.inputTextField.tintColor = self.inputTextField.textColor;
    self.inputTextField.placeholderColor = [UIColor qmui_colorWithHexString:@"#D6D6D6"];
    self.inputTextField.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
    self.inputTextField.textInsets = UIEdgeInsetsMake(0, 10, 0, 10);
    [self addSubview:self.inputTextField];
    [self.inputTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.titleLabel.mas_bottom);
        make.left.mas_equalTo(self.titleLabel);
        make.right.mas_equalTo(self).offset(-10.0);
        make.height.mas_equalTo(BCTitleInputViewInputHeight);
    }];
    //
    self.bottomLine = [UIView new];
    self.bottomLine.backgroundColor = [UIColor separatorColor];
    [self addSubview:self.bottomLine];
    [self.bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(10.0);
        make.right.mas_equalTo(self).offset(-10.0);
        make.height.mas_equalTo(1.0);
        make.bottom.mas_equalTo(self);
    }];
    
    //
    self.inputTextField.inputAccessoryView = [UIView new];
}

#pragma mark - noti
- (void)inputTextFieldTextDidChanged:(NSNotification *)noti {
    if (noti.object==self.inputTextField) {
        QMUITextField *textFiled = self.inputTextField;
        if (self.delegate && [self.delegate respondsToSelector:@selector(titleInputView:textFieldTextDidChanged:)]) {
            [self.delegate titleInputView:self textFieldTextDidChanged:textFiled];
        }
    }
}


- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:self.inputTextField];
}


+ (CGFloat)defaultHeight {
    CGFloat height = 0;
    height += BCTitleInputViewTopPadding;
    height += BCTitleInputViewTitleHeight;
    height += BCTitleInputViewInputHeight;
    return height;
}

@end
